package com.cn.mmd.controller;

import com.cn.mmd.model.Course;
import com.cn.mmd.model.CourseWeekly;
import com.cn.mmd.model.StudentApp;
import com.cn.mmd.model.StudentCourseRubLesson;
import com.cn.mmd.service.CourseService;
import com.cn.mmd.service.CourseWeeklyService;
import com.cn.mmd.service.StudentCourseService;
import com.cn.mmd.service.StudentRubLessonService;
import com.cn.mmd.tools.SplitSpring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生蹭课
 */

@Controller
@RequestMapping(value = "page/rubLesson")
public class StudentRubLessonController {

    @Autowired
    private StudentRubLessonService studentRubLessonService;

    @Autowired
    private CourseWeeklyService courseWeeklyService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentCourseService studentCourseService;

    private SplitSpring splitSpring = new SplitSpring();

    @Autowired
    private TokenController tokenController;

    /**
     * 按照老师评分查询 并排列
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectGrade")
    @ResponseBody
    public synchronized Object rubLessonGrade(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        Course course;
        String week1;
        int n;
        int m;
        List<CourseWeekly> courseWeekly;
        StudentCourseRubLesson studentCourseRubLesson = new StudentCourseRubLesson("已评分", 0, 4);
        List<StudentCourseRubLesson> studentCourseRubLessonList = studentRubLessonService.countByGrade(studentCourseRubLesson);
        if (studentCourseRubLessonList != null) {
            for (int i = 0; i < studentCourseRubLessonList.size(); i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                studentCourseRubLesson = studentCourseRubLessonList.get(i);
                course = courseService.selectByKey(studentCourseRubLesson.getCourseid());
                map.put("courseId", course.getCourseid());
                map.put("courseTeacher", course.getCourseteacher());
                map.put("gradeNum", studentCourseRubLesson.getGrade());
                mapList.add(map);
                courseWeekly = courseWeeklyService.selectList(studentCourseRubLesson.getCourseid());
                for (int j = 0; j < courseWeekly.size(); j++) {
                    week1 = courseWeekly.get(j).getCourseweeklyweek();
                    if (!week1.equals("")) {
                        if (splitSpring.splitSpringBy(week1, 0, 0, "第", 2).length > 1) {
                            week1 = splitSpring.splitSpringBy(week1, 0, 0, "第", 2)[1];
                            week1 = splitSpring.splitSpringBy(week1, 0, 0, "周", 2)[0];
                            n = Integer.parseInt(splitSpring.splitSpringBy(week1, 0, 0, "-", 2)[0]);
                            m = Integer.parseInt(splitSpring.splitSpringBy(week1, 0, 0, "-", 2)[1]);
                            map = new HashMap<String, Object>();
                            map.put("weeklyDay", courseWeekly.get(j).getCourseweeklyday());
                            map.put("weeklyQuarter", courseWeekly.get(j).getCourseweeklyquarter());
                            map.put("beginEndWeek", n + "-" + m);
                            map.put("classRoom", courseWeekly.get(j).getCourseweeklyclassroom());
                            map.put("courseName", course.getCoursename());
                            map.put("courseWeeklyId", courseWeekly.get(j).getCourseweeklyid());
                            mapList.add(map);
                        }
                    }
                }

            }
        } else {

        }
        if (mapList != null && mapList.size() > 0) {
            objectMap.put("code", 200);
            objectMap.put("message", "成功");
            objectMap.put("data", mapList);
        } else {
            objectMap.put("code", 201);
            objectMap.put("message", "查看失败");
        }
        return objectMap;
    }

    /**
     * 按照已添加 就是已报名查询并排列
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectType")
    @ResponseBody
    public synchronized Object rubLessonType(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String studentId = request.getParameter("studentId");
        int startrow = Integer.parseInt(request.getParameter("startrow"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String token = request.getParameter("token");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(studentId, token);
        if (studentApp != null) {
            List<Map> mapList = new ArrayList<Map>();
            Course course;
            List<Course> courseList;
            if (startrow < 1) {
                startrow = 1;
            }
            startrow = (startrow - 1) * pageSize;
            StudentCourseRubLesson studentCourseRubLesson = new StudentCourseRubLesson(startrow, pageSize, "已添加");
            List<StudentCourseRubLesson> studentCourseRubLessonList = studentRubLessonService.countByType(studentCourseRubLesson);
            if (studentCourseRubLessonList != null && studentCourseRubLessonList.size() > 0) {
                if (studentCourseRubLessonList.size() == pageSize) {
                    for (int i = 0; i < pageSize; i++) {
                        Map<String, Object> map;
                        studentCourseRubLesson = studentCourseRubLessonList.get(i);
                        course = courseService.selectByKey(studentCourseRubLesson.getCourseid());
                        map = new HashMap<String, Object>();
                        map.put("courseId", course.getCourseid());
                        map.put("courseTeacher", course.getCourseteacher());
                        map.put("TypeNum", studentCourseRubLesson.getType());
                        map.put("courseName", course.getCoursename());
                        map.put("courseTime", course.getCoursetime());
                        studentCourseRubLesson = new StudentCourseRubLesson();
                        studentCourseRubLesson.setStudentid(studentId);
                        studentCourseRubLesson.setCourseid(course.getCourseid());
                        if (studentRubLessonService.selectByStuIdCouId(studentCourseRubLesson) != null) {
                            map.put("courseType", 1);
                        } else {
                            map.put("courseType", 0);
                        }
                        mapList.add(map);
                    }
                } else {
                    for (int i = 0; i < studentCourseRubLessonList.size(); i++) {
                        Map<String, Object> map;
                        studentCourseRubLesson = studentCourseRubLessonList.get(i);
                        course = courseService.selectByKey(studentCourseRubLesson.getCourseid());
                        map = new HashMap<String, Object>();
                        map.put("courseId", course.getCourseid());
                        map.put("courseTeacher", course.getCourseteacher());
                        map.put("TypeNum", studentCourseRubLesson.getType());
                        map.put("courseName", course.getCoursename());
                        map.put("courseTime", course.getCoursetime());
                        studentCourseRubLesson = new StudentCourseRubLesson();
                        studentCourseRubLesson.setStudentid(studentId);
                        studentCourseRubLesson.setCourseid(course.getCourseid());
                        if (studentRubLessonService.selectByStuIdCouId(studentCourseRubLesson) != null) {
                            map.put("courseType", 1);
                        } else {
                            map.put("courseType", 0);
                        }
                        mapList.add(map);
                    }
                    course = new Course();
                    course.setCourseclasss("");
                    courseList = studentCourseService.selectClasss(course);
                    int max = pageSize - studentCourseRubLessonList.size();
                    int num1 = 0;
                    int i = 0;
                    Map<String, Object> map;
                    for (int j = 0; j < max; ) {
                        i++;
                        course = courseList.get(i);
//                    System.out.println(max + "," + num + "," + num1+courseList.get(j).getCourseid());
                        if (mapList.size() > 0) {
                            for (int maps = 0; maps < mapList.size(); maps++) {
                                if (course.getCourseid().equals(mapList.get(maps).get("courseId"))//&&course.getCourseteacher().equals(mapList.get(maps).get("courseTeacher"))
                                        ) {
                                    break;
                                } else {
                                    num1++;
                                    if (num1 == mapList.size()) {
                                        map = new HashMap<String, Object>();
                                        map.put("courseId", course.getCourseid());
                                        map.put("courseTeacher", course.getCourseteacher());
                                        map.put("TypeNum", 0);
                                        map.put("courseName", course.getCoursename());
                                        map.put("courseTime", course.getCoursetime());
                                        map.put("courseType", 0);
                                        mapList.add(map);
                                        num1 = 0;
                                        j++;
                                    }
                                }
                            }
                        } else {
                            studentCourseRubLesson = new StudentCourseRubLesson();
                            studentCourseRubLesson.setCourseid(course.getCourseid());
                            if (studentRubLessonService.selectByCouId(studentCourseRubLesson)!=null&&studentRubLessonService.selectByCouId(studentCourseRubLesson).size()==0){
                                map = new HashMap<String, Object>();
                                map.put("courseId", course.getCourseid());
                                map.put("courseTeacher", course.getCourseteacher());
                                map.put("TypeNum", 0);
                                map.put("courseName", course.getCoursename());
                                map.put("courseTime", course.getCoursetime());
                                map.put("courseType", 0);
                                mapList.add(map);
                                j++;
                            }
                        }
                    }
                }
            } else {
                course = new Course();
                course.setCourseclasss("");
                courseList = studentCourseService.selectClasss(course);
                int max = pageSize;
                Map<String, Object> map;
                int i = 0;
                for (int j = 0; j < max; j++) {
                    i++;
                    course = courseList.get(i);
                    studentCourseRubLesson = new StudentCourseRubLesson();
                    studentCourseRubLesson.setCourseid(course.getCourseid());
                    if (studentRubLessonService.selectByCouId(studentCourseRubLesson)!=null&&studentRubLessonService.selectByCouId(studentCourseRubLesson).size()==0){
                        map = new HashMap<String, Object>();
                        map.put("courseId", course.getCourseid());
                        map.put("courseTeacher", course.getCourseteacher());
                        map.put("TypeNum", 0);
                        map.put("courseName", course.getCoursename());
                        map.put("courseTime", course.getCoursetime());
                        map.put("courseType", 0);
                        mapList.add(map);
                    }else {
                        j--;
                    }
                }
            }
            if (mapList != null && mapList.size() > 0) {
                objectMap.put("code", 200);
                objectMap.put("message", "成功");
                objectMap.put("data", mapList);
            } else {
                objectMap.put("code", 201);
                objectMap.put("message", "查看失败");
            }
        } else {
            objectMap.put("code", 202);
            objectMap.put("message", "此用户没有权限做此操作");
        }
        return objectMap;
    }

    /**
     * 选择蹭课 课程
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/addType")
    @ResponseBody
    public synchronized Object addType(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        synchronized (objectMap) {
            String studentId = request.getParameter("studentId");
            String teacherName = request.getParameter("teacherName");
            String courseId = request.getParameter("courseId");
            String token = request.getParameter("token");
            StudentApp studentApp;
            studentApp = (StudentApp) tokenController.token1(studentId, token);
            if (studentApp != null) {
                StudentCourseRubLesson studentCourseRubLesson = new StudentCourseRubLesson();
                studentCourseRubLesson.setStudentid(studentId);
                studentCourseRubLesson.setCourseid(courseId);
                studentCourseRubLesson.setTeacherid(teacherName);
                studentCourseRubLesson.setType("已添加");
                if (studentRubLessonService.selectByStuIdCouId(studentCourseRubLesson) == null) {
                    if (studentRubLessonService.insertSelective(studentCourseRubLesson) > 0) {
                        objectMap.put("code", 200);
                        objectMap.put("message", "成功");
                    } else {
                        objectMap.put("code", 201);
                        objectMap.put("message", "添加失败");
                    }
                } else {
                    objectMap.put("code", 202);
                    objectMap.put("message", "不可重复添加");
                }
            } else {
                objectMap.put("code", 203);
                objectMap.put("message", "此用户没有权限做此操作");
            }
            return objectMap;
        }
    }

    /**
     * 查看自己蹭课的课程
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectRubLesson")
    @ResponseBody
    public synchronized Object selectRubLesson(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String studentId = request.getParameter("studentId");
        StudentCourseRubLesson studentCourseRubLesson = new StudentCourseRubLesson();
        studentCourseRubLesson.setStudentid(studentId);
        List<StudentCourseRubLesson> studentCourseRubLessonList = studentRubLessonService.selectByStuId(studentCourseRubLesson);
        if (studentCourseRubLessonList != null && studentCourseRubLessonList.size() > 0) {
            List<Map> mapList = new ArrayList<Map>();
            Map<String, Object> map;
            Course course;
            List<CourseWeekly> courseWeekly;
            String week1;
            int n;
            int m;
            for (int i = 0; i < studentCourseRubLessonList.size(); i++) {
                studentCourseRubLesson = studentCourseRubLessonList.get(i);
                course = courseService.selectByKey(studentCourseRubLesson.getCourseid());
                courseWeekly = courseWeeklyService.selectList(studentCourseRubLesson.getCourseid());
                for (int j = 0; j < courseWeekly.size(); j++) {
                    week1 = courseWeekly.get(j).getCourseweeklyweek();
                    if (!week1.equals("")) {
                        if (splitSpring.splitSpringBy(week1, 0, 0, "第", 2).length > 1) {
                            week1 = splitSpring.splitSpringBy(week1, 0, 0, "第", 2)[1];
                            week1 = splitSpring.splitSpringBy(week1, 0, 0, "周", 2)[0];
                            n = Integer.parseInt(splitSpring.splitSpringBy(week1, 0, 0, "-", 2)[0]);
                            m = Integer.parseInt(splitSpring.splitSpringBy(week1, 0, 0, "-", 2)[1]);
                            map = new HashMap<String, Object>();
                            map.put("courseId", course.getCourseid());
                            map.put("courseTeacher", studentCourseRubLessonList.get(i).getTeacherid());
                            map.put("weeklyDay", courseWeekly.get(j).getCourseweeklyday());
                            map.put("weeklyQuarter", courseWeekly.get(j).getCourseweeklyquarter());
                            map.put("beginEndWeek", n + "-" + m);
                            map.put("classRoom", courseWeekly.get(j).getCourseweeklyclassroom());
                            map.put("courseName", course.getCoursename());
                            map.put("courseWeeklyId", courseWeekly.get(j).getCourseweeklyid());
                            mapList.add(map);
                        }
                    }
                }
            }
            objectMap.put("code", 200);
            objectMap.put("message", "成功");
            objectMap.put("data", mapList);
        } else {
            objectMap.put("code", 201);
            objectMap.put("message", "没有蹭课记录");
        }

        return objectMap;
    }

    /**
     * 删除自己蹭课的课程
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteType")
    @ResponseBody
    public synchronized Object deleteType(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        synchronized (objectMap) {
            String studentId = request.getParameter("studentId");
            String courseId = request.getParameter("courseId");
            StudentCourseRubLesson studentCourseRubLesson = new StudentCourseRubLesson();
            synchronized (studentCourseRubLesson) {
                studentCourseRubLesson.setStudentid(studentId);
                studentCourseRubLesson.setCourseid(courseId);
            }
            if (studentRubLessonService.selectByStuIdCouId(studentCourseRubLesson) != null
                    && studentRubLessonService.deleteByStuIdCouId(studentCourseRubLesson) > 0) {
                objectMap.put("code", 200);
                objectMap.put("message", "成功");
            } else {
                objectMap.put("code", 201);
                objectMap.put("message", "删除失败");
            }

            return objectMap;
        }
    }
}
